home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / regsub.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  2.2 KB  |  91 lines

  1. /*
  2.  * regsub
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  */
  21. #include <stdio.h>
  22. #include <regexp.h>
  23. #include <string.h>
  24. /*
  25.  * The first byte of the regexp internal "program" is actually this magic
  26.  * number; the start node begins in the second byte.
  27.  */
  28. #define    MAGIC    0234
  29.  
  30.  
  31. #define CHARBITS 0377
  32. #ifndef CHARBITS
  33. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  34. #else
  35. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  36. #endif
  37.  
  38. /*
  39.  - regsub - perform substitutions after a regexp match
  40.  */
  41. void regsub(prog, source, dest)
  42. regexp *prog;
  43. char *source;
  44. char *dest;
  45. {
  46.     register char *src;
  47.     register char *dst;
  48.     register char c;
  49.     register int no;
  50.     register int len;
  51. #ifndef __STDC__    /* for __STDC__ pick up proto from <std.h> */
  52.     extern char *strncpy();
  53. #endif
  54.  
  55.  
  56.     if (prog == NULL || source == NULL || dest == NULL) {
  57.         regerror("NULL parm to regsub");
  58.         return;
  59.     }
  60.     if (UCHARAT(prog->program) != MAGIC) {
  61.         regerror("damaged regexp fed to regsub");
  62.         return;
  63.     }
  64.  
  65.     src = source;
  66.     dst = dest;
  67.     while ((c = *src++) != '\0') {
  68.         if (c == '&')
  69.             no = 0;
  70.         else if (c == '\\' && '0' <= *src && *src <= '9')
  71.             no = *src++ - '0';
  72.         else
  73.             no = -1;
  74.  
  75.         if (no < 0) {    /* Ordinary character. */
  76.             if (c == '\\' && (*src == '\\' || *src == '&'))
  77.                 c = *src++;
  78.             *dst++ = c;
  79.         } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  80.             len = (int)(prog->endp[no] - prog->startp[no]);
  81.             strncpy(dst, prog->startp[no], len);
  82.             dst += len;
  83.             if (len !=0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
  84.                 regerror("damaged match string");
  85.                 return;
  86.             }
  87.         }
  88.     }
  89.     *dst++ = '\0';
  90. }
  91.